import request from "@/axios";
import globle from "@/common/global"
import Vue from 'vue'
export default ({
    namespaced:true,
    state: {
        //主页推荐文章
        RecommendArticle:[],
        RecommendArticleState:0,  // 0:无状态 1:加载 2:到底了
        RecommendArticleInfo:{pageNo:1,pageSize:3,length:0,total:0,type:'recommend'},  
    },
    getters: {},
    mutations: {
       

        //设置主页推荐文章
        SETRECOMMENDARTICLE(state,{records,total}){     
     
            records.forEach((item,index)=>{		
                item.image=item.image?.split(',')//获取图片列表

                item.direction=((state.RecommendArticle.length) % 2 == 0)?1:2 //1:左侧 2:右侧
                if(item.content)
				item.content=globle.getContent(item.content) //解析内容
                
                item.tags = item.extra?.tags?.split(',') //获取标签
                state.RecommendArticle.push(Object.assign({},item))
			})
	      
            state.RecommendArticleInfo.pageNo+=1 //页数
            state.RecommendArticleInfo.total=total //总数
            state.RecommendArticleInfo.length=state.RecommendArticle.length //当前总长度

            if(state.RecommendArticleInfo.length == total)
             state.RecommendArticleState=2  //加载完了,到底了
            else  state.RecommendArticleState=0
        },

        //设置文章状态
        SETRECOMMENDAERICLESTATE(state,index){
            state.RecommendArticleState=index
        },

           

    },
    actions: {
    
       //获取主页推荐文章信息
       async GetRecommendArticle(context) {  
            let {pageNo,pageSize,type} = context.state.RecommendArticleInfo 
            context.commit('SETRECOMMENDAERICLESTATE',1) //设置虚拟列表正在加载
            try{
                let res= await  request.get('/api/article/get',{params:{type,pageNo,pageSize}})	
                setTimeout(() => {
                     //延迟加载
                     if(res.data.code==200) context.commit('SETRECOMMENDARTICLE',res.data.data)
                }, 200);
                return res.data 
            }catch(error){  return {message:error,code:400}  }
       },
    }
})

